<?php
namespace app\platform\controller;
use app\core\controller\core;
use app\core\controller\Error;

/**
 * @title 总平台接口
 * @description 总平台接口
 */
class Index
{

    private $core,$err;

    function __construct()
    {
        //加载core类
        $this->core = new core();
        //加载Error类
        $this->re = new Error();
    }

    /**
     * @title 登录
     * @description 总平台登录
     * @author roy
     * @url /platform/index/index
     * @method POST
     * @param name:mobile type:string require:1 default:0 other: desc:手机号
     * @param name:type type:int require:1 default:0 other: desc:类型
     *
     */
    public function index()
    {
        //输入
        $bool = $this->re->auto('post');
        if($bool==false){
            return  $this->re->out('999996');
        }
        //入口数据
        $in = $this->re->in;
        //定义必传值,isZero为1时表示不能传空或零，为0时表示不能传空。
        $key=[
            'mobile'=>['isZero'=>1],
            'type'=>['isZero'=>0],
        ];
        $em = $this->re->will($key,$in);
        if($em['code']!='000000'){
            return  $this->re->out($em['code'],$em['msg'],1);
        }
        //-----------------------------------------------------------------------------------



        //-----------------------------------------------------------------------------------
        //输出:
        $this->re->data = $in;
        return  $this->re->out();
    }

    //检测围栏
    function map(){
        $aa = "120.199632,30.21117|120.20476,30.20288|120.206863,30.197539|120.207721,30.196427|120.210339,30.196464|120.210403,30.195759|120.210596,30.192356|120.210768,30.186903|120.210961,30.181616|120.211347,30.175644|120.212313,30.175662|120.233277,30.174883|120.233438,30.174892|120.23346,30.172666|120.234017,30.169457|120.234329,30.167546|120.229114,30.164791|120.218386,30.1462|120.21624,30.143009|120.209974,30.143621|120.208537,30.143696|120.207957,30.143789|120.207335,30.143825|120.206777,30.143862|120.20343,30.143974|120.201262,30.143937|120.198837,30.143825|120.196842,30.143658|120.192765,30.14941|120.189504,30.147741|120.188216,30.146739|120.182937,30.145031|120.18195,30.147926|120.177251,30.148381|120.176339,30.15115|120.175196,30.150558|120.17439,30.15023|120.173778,30.14953|120.172359,30.148724|120.171093,30.148334|120.166115,30.149299|120.162467,30.148928|120.157746,30.145699|120.158016,30.145575|120.157104,30.14519|120.156567,30.144619|120.155902,30.14435|120.155269,30.144239|120.154829,30.144155|120.154507,30.143951|120.154475,30.143283|120.15425,30.143098|120.153488,30.142792|120.152866,30.142708|120.152297,30.142411|120.151847,30.141947|120.151686,30.141585|120.151063,30.141019|120.150109,30.140472|120.139165,30.149536|120.129724,30.164824|120.12878,30.176549|120.12938,30.183597|120.134788,30.188346|120.151611,30.193316|120.181995,30.204666|120.199461,30.211601|120.200062,30.210452";
        //转换坐标
        $s = explode('|',$aa);
        foreach($s as $k => $v){
            $vv = explode(',',$v);
            $map[$k] = [
                'lng' => $vv[0],
                'lat' => $vv[1]
            ];
        }

        //120.10476,30.145314
        $p = [
            'lng' => '120.10476',
            'lat' => '30.145314'
        ];

        //120.183037,30.179599
        $p1 = [
            'lng' => '120.183037',
            'lat' => '30.179599'
        ];

        echo "p:120.159713,30.179042->".Fun::isInsidePolygons($p,$map).",,,,p1:120.149971,30.232079->".Fun::isInsidePolygons($p1,$map);

        var_dump(Fun::isInsidePolygons($p,$map));

        var_dump(Fun::isInsidePolygons($p1,$map));
    }


    //添加终端
    public function entity_add(){
        $url = 'http://yingyan.baidu.com/api/v3/entity/add';

        $body = [
            'ak'=>'MNpU43npPzqnFuQX2wmvZGG3AeGge5pC',
            'service_id'=>154742,
            'entity_name'=>'s1_1',
            'entity_desc'=>'我的',
        ];

        return $this->cUrl($url,$body);
    }

    //添加终端对应的坐标
    public function addpoint(){
        $url = 'http://yingyan.baidu.com/api/v3/track/addpoint';
        $body = [
            'ak'=>'MNpU43npPzqnFuQX2wmvZGG3AeGge5pC',
            'service_id'=>154742,
            'entity_name'=>'s1_1',
            'latitude'=>'30.271326',
            'longitude'=>'120.196188',
            'loc_time'=>time(),
            'coord_type_input'=>'bd09ll',

        ];
        return $this->cUrl($url,$body);
    }

    //创建多边型围栏
    public function yingyan_create(){
        $a = '[{"lng":121.431327,"lat":28.654776},{"lng":121.429209,"lat":28.647802},{"lng":121.423422,"lat":28.642697},{"lng":121.415517,"lat":28.640828},{"lng":121.407612,"lat":28.642697},{"lng":121.401825,"lat":28.647802},{"lng":121.399707,"lat":28.654776},{"lng":121.401825,"lat":28.661749},{"lng":121.407612,"lat":28.666853},{"lng":121.415517,"lat":28.668722},{"lng":121.423422,"lat":28.666853},{"lng":121.429209,"lat":28.661749}]';

        $a = json_decode($a,true);
        foreach ($a as $k=>$v){
            if($k==0){
                $aa=$v['lat'].",".$v['lng'];
            }else{
                $aa.=";".$v['lat'].",".$v['lng'];
            }

        }
        echo $aa;
//        exit;

        $url = "http://yingyan.baidu.com/api/v3/fence/createpolygonfence";

        $body = [
            'ak'=>'MNpU43npPzqnFuQX2wmvZGG3AeGge5pC',
            'service_id'=>154742,
            'fence_name'=>'',
            'monitored_person'=>'s1_1',
            'vertexes'=>$aa,
            'coord_type'=>'bd09ll',
            'denoise'=>10,
        ];

        return $this->cUrl($url,$body);
    }


    //检测是否在范围内
    public function querystatus(){
        $url="http://yingyan.baidu.com/api/v3/fence/querystatus";
        $url .= "?ak=MNpU43npPzqnFuQX2wmvZGG3AeGge5pC&service_id=154742&monitored_person=s1_1";
        $body=[
            'ak'=>'MNpU43npPzqnFuQX2wmvZGG3AeGge5pC',
            'service_id'=>154742,
            'monitored_person'=>'s1_1',
        ];
        return $this->get($url);
    }

    private function cUrl($url,$body){
//        $url = "http://localhost/web_services.php";
//        $post_data = array ("username" => "bob","key" => "12345");

        $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // post数据
    curl_setopt($ch, CURLOPT_POST, 1);
    // post的变量
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

    $output = curl_exec($ch);
    curl_close($ch);

    //打印获得的数据
    return $output;
    }


    private function get($url){
        //初始化
        $ch = curl_init();
    //设置选项，包括URL
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    //执行并获取HTML文档内容
    $output = curl_exec($ch);
    //释放curl句柄
    curl_close($ch);
    //打印获得的数据
    return $output;
    }

}
